package cn.thoughtworks.school.entities;

import com.fasterxml.jackson.annotation.JsonManagedReference;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.hankcs.hanlp.HanLP;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import javax.persistence.*;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

@Entity
@Table(name = "subjectiveQuiz")

@Setter
@Getter
@NoArgsConstructor
public class SubjectiveQuiz {

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Long id;
  private String description;
  private Long makerId;
  private Long referenceNumber;
  private Boolean isAvailable;
  private Long quizGroupId;
  private String createTime;
  private String updateTime;
  private String remark;
  private Long organizationId;
  private String referenceAnswer;
  @ManyToMany(cascade = CascadeType.ALL)
  @JoinTable(name = "subjectiveQuiz_tags",
    joinColumns = {@JoinColumn(name = "subjectiveQuizId")},
    inverseJoinColumns = {@JoinColumn(name = "tagId")})
  @JsonManagedReference
  private Set<Tag> tags = new HashSet<>();

  public SubjectiveQuiz(String description, Long creatorId, String remark, Long organizationId, String referenceAnswer) {
    this.description = description;
    this.createTime = new SimpleDateFormat("YYYY-MM-dd HH-mm-ss").format(new Date());
    this.makerId = creatorId;
    this.isAvailable = true;
    this.remark = remark;
    this.organizationId = organizationId;
    this.referenceNumber = 0L;
    this.referenceAnswer = referenceAnswer;
  }

  public static SubjectiveQuiz copy(SubjectiveQuiz subjectiveQuiz, Long creatorId, Long organizationId) {
    return new SubjectiveQuiz(subjectiveQuiz.getDescription(), creatorId, subjectiveQuiz.getRemark(), organizationId, subjectiveQuiz.getReferenceAnswer());
  }

  @JsonProperty("description_zh_TW")
  public String getDescriptionTW() {
    return Objects.isNull(description) ? "" : HanLP.s2tw(description);
  }
}
